package prog1.helpers;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.*;
import java.security.cert.CertificateException;

/**
 * Created with IntelliJ IDEA.
 * User: Roi
 * Date: 07/12/13
 *
 * This class is responsible for the interaction with the key store.
 */
public class KeyStoreProxy {

    private KeyStore keyStore;

    /**
     * Initializes this object with type and provider
     * @param type key store type
     * @param provider key store provider
     * @throws NoSuchProviderException
     * @throws KeyStoreException
     */
    public void initialize(String type, String provider)
            throws NoSuchProviderException, KeyStoreException {
        keyStore = KeyStore.getInstance(type, provider);
    }

    /**
     * Loads key store from filename using given password
     * @param filename key store filename
     * @param password key store password
     * @throws IOException
     * @throws CertificateException
     * @throws NoSuchAlgorithmException
     */
    public void load(String filename, String password)
            throws IOException, CertificateException,
            NoSuchAlgorithmException {
        FileInputStream stream = new FileInputStream(filename);
        keyStore.load(stream, password.toCharArray());
        stream.close();
    }

    /**
     * Saves key store to filename using given password
     * @param filename key store filename
     * @param password key store password
     * @throws IOException
     * @throws CertificateException
     * @throws NoSuchAlgorithmException
     * @throws KeyStoreException
     */
    public void save(String filename, String password)
            throws IOException, CertificateException,
            NoSuchAlgorithmException, KeyStoreException {
        keyStore.store(new FileOutputStream(filename), password.toCharArray());
    }

    /**
     * @return public key according to given alias
     * @throws Exception
     */
    public PublicKey getPublicKey(String alias)
            throws Exception {
        return keyStore.getCertificate(alias).getPublicKey();
    }

    /**
     * @return private key according to given alias using given password
     * @throws Exception
     */
    public PrivateKey getPrivateKey(String alias, String password)
            throws Exception {
        return (PrivateKey) getKey(alias, password);
    }

    /**
     * @return key according to given alias using given password
     * @throws Exception
     */
    public Key getKey(String alias, String password)
            throws Exception {
        return keyStore.getKey(alias, password.toCharArray());
    }

    /**
     * Sets entry in this key store
     * @param alias alias
     * @param entry entry to set
     * @param protectionParameter protection parameter
     * @throws KeyStoreException
     */
    public void setEntry(String alias, KeyStore.Entry entry, KeyStore.ProtectionParameter protectionParameter)
            throws KeyStoreException {
        keyStore.setEntry(alias, entry, protectionParameter);
    }
}
